package com.by.wx.job;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson2.JSON;
import com.by.wx.mapper.FlightRecordLogMapper;
import com.by.wx.model.msg.FlightRecordLog;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.Duration;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author 陈子杰
 * @Description
 * @Version 1.0.0
 * @Date 2025/4/25 15:27
 */
@Slf4j
@Component
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class CheckInRemindJob {
    @Value("${feishu.url}")
    private String url;
    private final FlightRecordLogMapper flightRecordLogMapper;

//    @Scheduled(cron = "*/30 * * * * ?")
    @Scheduled(cron = "0 */5 * * * ?")
    public void checkIn() {
        List<FlightRecordLog> logList = flightRecordLogMapper.selectList(null);
        if (CollectionUtil.isEmpty(logList)) {
            return;
        }
        for (FlightRecordLog recordLog : logList) {
            LocalDateTime lastTime = recordLog.getLastTime();
            LocalDateTime now = LocalDateTime.now();
            Duration duration = Duration.between(lastTime, now);
            long minutes = duration.toMinutes();
            if (minutes > 40) {
                Map<String, Object> body = new HashMap<>();
                body.put("msg_type", "text");
                Map<String, Object> innerMap = new HashMap<>();
                String msg = String.format("设备：%s，长时间未运行新订单", recordLog.getDeviceName());
                innerMap.put("text", msg);
                body.put("content", innerMap);
                HttpUtil.post(url, JSON.toJSONString(body));
            }
        }
    }
}
